package com.education.module.system.user;

import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.User;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;
import com.education.framework.util.Md5Util;
import com.education.module.system.sysRole.SysRoleServices;

@Controller
@RequestMapping("system/user")
public class UserController extends BaseController{

	@Autowired
	private UserServices userServices;
	@Autowired
	private SysRoleServices sysRoleServices;
	
	@RequestMapping(value = "")
	public String list(Model model, SearchParams searchParams,Page page,ServletRequest request){
		List<User> list = userServices.find(searchParams,page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("searchParams", searchParams);
		return "/system/user/userList";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		User user = new User();
		model.addAttribute("user",user);
		model.addAttribute("roleList",sysRoleServices.find());
		model.addAttribute("action", "create");
		return "/system/user/userEdit";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(User user, Model model,RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		user.setCreateUser(sessionUser);
		user.setUpdateUser(sessionUser);
		if(userServices.findIsExist(user.getLoginName())){
			redirectAttributes.addFlashAttribute("MESSAGE", "保存失败，登录账号已经存在！");
			return "redirect:/system/user/create";
		}
		userServices.save(user);
		redirectAttributes.addFlashAttribute("MESSAGE", "操作成功！");
		return "redirect:/system/user";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		User user = userServices.findForObject(id);
		model.addAttribute("user", user);
		model.addAttribute("roleList",sysRoleServices.find());
		model.addAttribute("action", "update");
		return "/system/user/userEdit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("preloadFlow") User user, RedirectAttributes redirectAttributes) {
		userServices.update(user);
		return "redirect:/system/user";
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Integer id, RedirectAttributes redirectAttributes) {
		userServices.delete(id);
		return "redirect:/system/user";
	}
	
	@RequestMapping(value = "editUserPasswd", method = RequestMethod.GET)
	public String editUserPasswd(Model model) {
		model.addAttribute("user", SessionHelper.getInstance().getUser());
		return "/system/user/editUserPasswd";
	}
	@RequestMapping(value = "saveUserPasswd", method = RequestMethod.POST)
	public String saveUserPasswd(@ModelAttribute("preloadFlow") User user,Model model, RedirectAttributes redirectAttributes) {
		System.out.println("user.getOldpasswd()=" + user.getOldpasswd());
		User sessionUser = SessionHelper.getInstance().getUser();
		if(Md5Util.MD5Encode(user.getOldpasswd()).equals(sessionUser.getPasswd())){
			if(userServices.updatePasswd(sessionUser.getId(),user.getPasswd())){
				sessionUser.setPasswd(user.getPasswd());
				SessionHelper.getInstance().putUser(sessionUser);
				redirectAttributes.addFlashAttribute("MESSAGE", "操作成功！");
			}else{
				redirectAttributes.addFlashAttribute("MESSAGE", "操作失败！");
			}			
		}else{
			redirectAttributes.addFlashAttribute("MESSAGE", "原密码录入有误！");
		}
		
		return "redirect:/system/user/editUserPasswd";
	}
	
}
